package com.mrd.gtimp.init;

import com.mrd.gtimp.business.entity.CargoDeclaRecord;
import com.mrd.gtimp.business.service.cargoDeclaration.component.CargoDeclarationHelper;
import com.mrd.gtimp.business.repository.CargoDeclaRecordDao;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dengdc on 2017/5/26.
 */
public class ResendCargoDeclaRunnable implements Runnable  {
    private CargoDeclarationHelper cargoDeclarationHelper;
    private CargoDeclaRecordDao cargoDeclaRecordDao;
    private Logger logger = LoggerFactory.getLogger(QueryCargoDeclaRunnable.class);

    public ResendCargoDeclaRunnable(ApplicationContext applicationContext){
        this.cargoDeclarationHelper = applicationContext.getBean(CargoDeclarationHelper.class);
        this.cargoDeclaRecordDao = applicationContext.getBean(CargoDeclaRecordDao.class);
    }

    @Override
    public void run() {
        List<String> queryStatus = new ArrayList<>();
        queryStatus.add("-1");//上传失败
        queryStatus.add("0");//初始化
        while (true){
            List<CargoDeclaRecord> records = cargoDeclaRecordDao.findByStatusIn(queryStatus);
            if(CollectionUtils.isNotEmpty(records)){
                for (CargoDeclaRecord record : records) {
                    try {
                        cargoDeclarationHelper.didUpload(record.getDid());
                    } catch (Exception e) {
                        logger.error("重发失败：" + e.getMessage() + "; 业务编码：" + record.getBusinessNo());
                        e.printStackTrace();
                    }
                }
            }
            try {
                Thread.sleep( 15 * 60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
